package cs551.listtracker.structure;

import java.util.ArrayList;
import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

public class History implements Parcelable {
	public static final Parcelable.Creator<History> CREATOR = new Parcelable.Creator<History>() {
		public History createFromParcel(Parcel source) {
			return new History(source);
		}

		@Override
		public History[] newArray(int size) {
			return new History[size];
		}
	};

	private List<ListItem> histList = new ArrayList<ListItem>();

	public History() {
//		histList.add(new ListItem("White Bread", 1, false));
//		histList.add(new ListItem("Brown Bread", 2, false));
//		histList.add(new ListItem("BreadSticks", 3, false));

	}

	public ListItem getHistory(String s) {
		if (s.equals("")) return null;

		for (ListItem item : histList) {
			if (item.title.equals(s))
				return item;
		}
		return null;

	}
	
	public void clear(){
		
		histList.clear();
		
	}

	public List<String> getStrings() {
		ArrayList<String> temp = new ArrayList<String>();

		for (ListItem item : histList) {
			temp.add(item.title);
		}

		return temp;
	}
	
	public void addHistory(ListItem newItem){
		for (ListItem item : histList) {
			
			if (item.title.equals(newItem.title)) item = newItem;
			return;
		}
		
		histList.add(newItem);
	}

	public History(Parcel source) {
		source.readList(histList, History.class.getClassLoader());
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		out.writeList(histList);

	}

	public void setHistory(Parcel parcel) {
		System.out.println("Set history");
		parcel.readList(histList, History.class.getClassLoader());
	}
}
